package com.qiche.tools

import java.text.SimpleDateFormat
import java.util.Calendar

object DateUtils {
  private val a_dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyyMMdd")
  private val b_dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
  private val c_dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  private val d_dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH")


  def getLastHour(h_diff: Int,m_diff:Int): String = {
    val cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.HOUR, -h_diff)
    cal.add(Calendar.MINUTE,-m_diff)
    val lastHour = c_dateFormat.format(cal.getTime())
    lastHour
  }

  def getNowTime(): String = {
    val cal: Calendar = Calendar.getInstance()
    val time = c_dateFormat.format(cal.getTime())

    time
  }

  def getZeroHour(diff:Int): String = {
    val cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.DATE, -diff)
    val lastHour = b_dateFormat.format(cal.getTime())
    val hourTimestamp = b_dateFormat.parse(lastHour).getTime
    val dtString = c_dateFormat.format(hourTimestamp)

    dtString
  }

  def getYesterdayHour(diff:Int,hours: Int,m_diff:Int): String = {
    val cal:Calendar = Calendar.getInstance()
    cal.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(getZeroHour(diff)))
    cal.add(Calendar.HOUR_OF_DAY,hours)
    cal.add(Calendar.MINUTE,-m_diff)
    return  c_dateFormat.format(cal.getTime)

  }


  def main(args: Array[String]): Unit = {
    println(getYesterdayHour(0,4,3))
    println(getLastHour(2,4))

    for(i<- 1 to 18)(println(scala.util.Random.nextInt(7)))
  }

}
